<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选项型文字互动游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            text-align: center;
        }
        #output {
            margin-bottom: 20px;
            white-space: pre-line;
            text-align: left;
            max-width: 600px;
            margin-left: auto;
            margin-right: auto;
        }
        .option {
            margin: 5px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            cursor: pointer;
            display: inline-block;
            background-color: #f0f0f0;
        }
        .option:hover {
            background-color: #e0e0e0;
        }
    </style>
</head>
<body>
    <div id="output"></div>
    <div id="options"></div>
    <script>
        // 游戏状态
        let gameState = {
            location: "森林入口",
            inventory: [],
            gameEnded: false
        };

        // 游戏场景和选项
        const scenes = {
            "森林入口": {
                description: "你站在一片神秘森林的入口。阳光透过树叶洒在地上，显得格外宁静。",
                options: [
                    { text: "进入森林", next: "森林深处1" },
                    { text: "查看背包", action: () => {
                        return `你的背包里有：${gameState.inventory.join(", ") || "空无一物"}`;
                    } }
                ]
            },
            "森林深处": {
                description: "你来到了森林深处，这里显得有些阴森。突然，你发现了一把剑。",
                options: [
                    { text: "拿起剑", action: () => {
                        gameState.inventory.push("剑");
                        return "你捡起了剑，感觉力量倍增！";
                    } },
                    { text: "探索洞穴", next: "神秘洞穴" },
                    { text: "返回入口", next: "森林入口" }
                ]
            },
            "神秘洞穴": {
                description: "你发现了一个神秘的洞穴入口，里面黑漆漆的，似乎隐藏着什么。",
                options: [
                    { text: "进入洞穴", next: "洞穴内部" },
                    { text: "返回森林深处", next: "森林深处" }
                ]
            },
            "洞穴内部": {
                description: "你进入了洞穴，里面很黑，但你发现了一只怪物！",
                options: [
                    { text: "与怪物战斗", action: () => {
                        if (gameState.inventory.includes("剑")) {
                            return "你用剑击败了怪物！你发现了一箱宝藏！游戏胜利！";
                        } else {
                            return "你没有武器，被怪物击败了！游戏结束。";
                        }
                    } },
                    { text: "逃跑", next: "神秘洞穴" }
                ]
            },
            "宝藏洞穴": {
                description: "你击败了怪物，发现了一箱闪闪发光的宝藏！但洞穴深处似乎还有东西。",
                options: [
                    { text: "检查宝藏", action: () => {
                        gameState.inventory.push("宝藏");
                        return "你将宝藏收入背包。";
                    } },
                    { text: "探索洞穴深处", next: "洞穴深处" },
                    { text: "离开洞穴", next: "森林深处" }
                ]
            },
            "洞穴深处": {
                description: "你来到了洞穴的最深处，发现了一个古老的祭坛。上面放着一本发光的魔法书。",
                options: [
                    { text: "拿起魔法书", action: () => {
                        gameState.inventory.push("魔法书");
                        return "你拿起魔法书，感觉到一股强大的魔法力量！";
                    } },
                    { text: "返回宝藏洞穴", next: "宝藏洞穴" }
                ]
            },
            "森林村庄": {
                description: "你走出了森林，来到了一个宁静的小村庄。村民们看起来很友好。",
                options: [
                    { text: "与村民交谈", next: "与村民交谈" },
                    { text: "进入森林深处", next: "森林深处" }
                ]
            },
            "与村民交谈": {
                description: "你与村民交谈后，得知村子里最近出现了一些奇怪的事情。他们需要帮助。",
                options: [
                    { text: "帮助村民", next: "帮助村民" },
                    { text: "不理他们，离开村庄", next: "森林入口" }
                ]
            },
            "帮助村民": {
                description: "你决定帮助村民。他们告诉你，最近森林里出现了一只邪恶的巨龙。",
                options: [
                    { text: "进入森林寻找巨龙", next: "寻找巨龙" },
                    { text: "返回村庄", next: "森林村庄" }
                ]
            },
            "寻找巨龙": {
                description: "你进入森林深处，寻找巨龙的踪迹。突然，你看到了巨龙的巢穴。",
                options: [
                    { text: "进入巢穴", next: "巨龙巢穴" },
                    { text: "返回村庄", next: "森林村庄" }
                ]
            },
            "巨龙巢穴": {
                description: "你进入了巨龙的巢穴，里面堆满了财宝。巨龙正在睡觉！",
                options: [
                    { text: "悄悄拿走财宝", action: () => {
                        gameState.inventory.push("财宝");
                        return "你成功拿走了财宝，巨龙没有醒来！";
                    } },
                    { text: "唤醒巨龙", action: () => {
                        return "你唤醒了巨龙，它愤怒地攻击你！你被击败了！游戏结束。";
                    } },
                    { text: "返回森林深处", next: "森林深处" }
                ]
            }
        };

        // 更新游戏界面
        function updateOutput(message) {
            const output = document.getElementById("output");
            output.textContent += message + "\n";
        }

        // 渲染选项
        function renderOptions(options) {
            const optionsContainer = document.getElementById("options");
            optionsContainer.innerHTML = ""; // 清空旧选项

            options.forEach(option => {
                const button = document.createElement("div");
                button.className = "option";
                button.textContent = option.text;

                button.addEventListener("click", () => {
                    if (option.action) {
                        const result = option.action();
                        updateOutput(result);
                        if (result.includes("游戏结束") || result.includes("游戏胜利")) {
                            gameState.gameEnded = true;
                        }
                    }
                    if (option.next) {
                        gameState.location = option.next;
                    }
                    renderScene();
                });

                optionsContainer.appendChild(button);
            });
        }

        // 渲染当前场景
        function renderScene() {
            const currentScene = scenes[gameState.location];
            updateOutput(currentScene.description);

            // 动态调整选项：如果剑已经被拿走，移除“拿起剑”选项
            let options = currentScene.options;
            if (gameState.location === "森林深处" && gameState.inventory.includes("剑")) {
                options = options.filter(option => option.text !== "拿起剑");
            }

            renderOptions(options);
        }

        // 游戏开始
        updateOutput("欢迎来到文字互动游戏！点击选项开始冒险。\n");
        renderScene();
    </script>
</body>
</html>